package com.ai.controller;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.common.constant.WseSectionConstants;
import com.ai.common.controller.BaseController;
import com.ai.common.exception.BusinessException;
import com.ai.common.model.Pagination;
import com.ai.common.model.Result;
import com.ai.model.edu.cms.info.dto.InfoDetailDTO;
import com.ai.model.edu.cms.info.vo.InfoDetailVO;
import com.ai.model.edu.cms.info.vo.InfoSectionVO;
import com.ai.service.cms.info.InfoDetailService;

/**
 * 
 * Class Name : IndexController<br>
 * 
 * Description : 首页Controller类<br>
 * 
 * @see
 *
 */
@Controller
public class IndexController extends BaseController {

	@Resource
	private InfoDetailService infoDetailService;

	/**
	 * Description ：首页
	 */
	@RequestMapping("/index")
	public String initArticle() {
		return "redirect:/login/loginindex";
	}

	/**
	 * 查询资讯内容（首页用）
	 * 
	 * @param infoDetailDTO
	 * @return
	 */
	@RequestMapping(value = "/front/findArticleList", method = RequestMethod.POST)
	@ResponseBody
	public Result doQueryArticle(@ModelAttribute InfoDetailDTO dto) {
		Result result = new Result(true);
		// 审核通过
		dto.setQueryStatus(Byte.valueOf("3"));
		if (dto.getRows() < 1) {
			dto.setRows(5);
		}
		try {
			Pagination<InfoDetailVO> infoList;
			switch (Integer.parseInt(dto.getLocChannelId().toString())) {
			case 100:
				infoList = infoDetailService.findInfoDetailPageHyByDto(dto, false);
				break;
			case 101:
				infoList = infoDetailService.findInfoDetailPageHyByDto(dto, false);
				break;
			case 102:
				infoList = infoDetailService.findInfoDetailPageHyByDto(dto, false);
				break;
			default:
				infoList = infoDetailService.findInfoDetailPageByDto(dto, false);
				break;
			}
			result.addAttribute("infoList", infoList.getRows());
		} catch (Exception e) {
			result.setSuccess(false);
		}

		return result;
	}

	/**
	 * 查询资讯内容（分类列表用）
	 * 
	 * @param infoDetailDTO
	 * @return
	 */
	@RequestMapping("/front/findArticleListBySection/{channelId}/{themeId}")
	public String findArticleListBySection(@PathVariable Long channelId, @PathVariable Long themeId,
			@RequestParam(required = false, defaultValue = "1") Integer page) {
		if (null == WseSectionConstants.sectionMap.get(channelId.toString())
				|| null == WseSectionConstants.sectionMap.get(themeId.toString())) {
			return "";
		}
		
		int rows = 10;
		
		Result result = new Result(true);
		InfoDetailDTO dto = new InfoDetailDTO();
		// 审核通过
		dto.setLocChannelId(channelId);
		dto.setLocThemeId(themeId);
		dto.setQueryStatus(Byte.valueOf("3"));
		dto.setRows(rows);
		dto.setPage(page);
		dto.setStart(page);
		
		Pagination<InfoDetailVO> infoList = infoDetailService.findInfoDetailPageByDto(dto, true);
		result.addAttribute("infoList", infoList.getRows());
		if(infoList.getTotal() == null){
			BusinessException.throwMessageWithCode("", "infoList.getTotal()为null");
		}
		int totalCount = infoList.getTotal().intValue();
		
		int totalPage = (totalCount % rows == 0) ? totalCount/rows : totalCount/rows + 1;
		result.addAttribute("totalPage", totalPage);
		result.addAttribute("totalCount", totalCount);
		result.addAttribute("page", page);

		// 1级，2级目录
		InfoSectionVO isVO = new InfoSectionVO();
		isVO.setCurrentChannelName(WseSectionConstants.sectionMap.get(channelId.toString()));
		isVO.setCurrentThemeName(WseSectionConstants.sectionMap.get(themeId.toString()));
		isVO.setLocChannelId(channelId);
		isVO.setLocThemeId(themeId);
		String themeStr = WseSectionConstants.menuMap.get(dto.getLocChannelId().toString());
		String[] themeArray = themeStr.split(",");
		Map<String, String> themeMap = new TreeMap<String, String>(new Comparator<String>() {
			public int compare(String obj1, String obj2) {
				// 升序排序
				return Long.valueOf(obj1).compareTo(Long.valueOf(obj2));
			}
		});
		for (int i = 0; i < themeArray.length; i++) {
			themeMap.put(themeArray[i], WseSectionConstants.sectionMap.get(themeArray[i]));
		}

		isVO.setThemeMap(themeMap);

		result.addAttribute("isVO", isVO);

		// 公告 start
		// 1级目录
		dto.setLocChannelId(Long.valueOf("6"));
		// 2级目录
		dto.setLocThemeId(Long.valueOf("29"));
		dto.setRows(5);
		dto.setPage(1);
		dto.setStart(1);
		Pagination<InfoDetailVO> noticeList = infoDetailService.findInfoDetailPageByDto(dto, false);
		result.addAttribute("noticeList", noticeList.getRows());
		// 公告 end
		result.rendering();
		return "/front/hyinfo";
	}
	
	/**
	 * 获取公告信息
	 * @return
	 */
	private void getNotice() throws Exception {
		Result result = new Result(true);
		InfoDetailDTO dto = new InfoDetailDTO();
		// 1级目录
		dto.setLocChannelId(Long.valueOf("6"));
		// 2级目录
		dto.setLocThemeId(Long.valueOf("29"));
		dto.setRows(5);
		dto.setPage(1);
		dto.setStart(1);
		Pagination<InfoDetailVO> noticeList = infoDetailService.findInfoDetailPageByDto(dto, false);
		if(noticeList == null){
			BusinessException.throwMessageWithCode("", "数据异常");
		}
		result.addAttribute("noticeList", noticeList.getRows());
		result.rendering();
	}
	
	/**
	 * 关于我们--简介
	 * 
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/about/{path}")
	public String findArticleListBySection(@PathVariable("path") String path) throws Exception {
		getNotice();
		return "/front/" + path;
	}

	
	@RequestMapping("/about/dcpt")
	public String findArticleListBySectionldzc() {
		return "/front/dcpt";
		}

}
